int getpid()
{
	return syscall(0, 0, 0, 0);
}

void ps()
{
	syscall(1, 0, 0, 0);
}

void chname()
{
	char newName[15];
	printf("What do you want to change it to: ");
	gets(newName);
	syscall(2, newName, 0, 0);
}

void kmode()
{
	syscall(3, 0, 0, 0);
}

void kswitch()
{
	syscall(4, 0, 0, 0);
}

int wait()
{
	int exitValue, pid;
	exitValue = syscall(5, 0, 0, 0);
	pid = getpid();
	printf("PID %d awoken with exit code %d\n", pid, exitValue);
}

void exit()
{
	char exitVal = 0;
	do
	{
		printf("  * Gimme exit value (0-9): ");
		exitVal = getc();
		printf("%c\n", exitVal);
	} while ((exitVal < '0') || (exitVal > '9'));
	syscall(6, (exitVal - '0'), 0, 0);
}

int fork()
{
	return syscall(7, 0, 0, 0);
}

void exec()
{
	char filename[30];
	printf("Input file name to execute: ");
	gets(filename);
	syscall(8, filename, 0, 0);
}

void upipe()
{
	int pfd[2];
	syscall(9, pfd, 0, 0);
	printf("Pipe returned %d and %d\n", pfd[0], pfd[1]);
}

void pfd()
{
	syscall(10, 0, 0, 0);
}

void close()
{
	int fd = 0;
	
	printf("Which FD? ");
	fd = geti();
	
	syscall(11, fd, 0, 0);
}

void read()
{
	int fd, nbytes, bytesRead;
	char buf[10];
	printf("Which FD? ");
	fd = geti();
	printf("How many bytes? ");
	nbytes = geti();
	bytesRead = syscall(12, fd, buf, nbytes);
	printf("Read %d bytes\n\"", bytesRead);
	if (bytesRead == 0)
	{
		printf("Pipe empty and no writers!\n");
		syscall(11, fd, 0, 0);
	}
	else
	{
		for (fd = 0; fd < bytesRead; ++fd)
		{
			printf("%c", buf[fd]);
		}
		printf("\"\n");
	}
}

void write()
{
	int fd, nbytes, bytesWritten;
	char buf[100];
	printf("Which FD? ");
	fd = geti();
	printf("Write what? ");
	gets(buf);
	bytesWritten = syscall(13, fd, buf, strlen(buf));
	printf("Wrote %d bytes\n\"", bytesWritten);
	if (bytesWritten == -1)
	{
		printf("Broken pipe error!\n");
		syscall(11, fd, 0, 0);
	}
	else
	{
		for (fd = 0; (fd < bytesWritten); ++fd)
		{
			printf("%c", buf[fd]);
		}
		printf("\"\n");
	}
}

void help()
{
	printf("-----------------------------------------------------\n");
	printf("| ps   chname   kmode   kswitch   wait   fork   pfd |\n");
	printf("| pipe   exec   close   pipe   pfd   close   read   |\n");
	printf("| write   exit                                      |\n");
	printf("-----------------------------------------------------\n");
}
